package com.twu.biblioteca;

import com.twu.biblioteca.model.Library;
import com.twu.biblioteca.model.LibraryService;
import com.twu.biblioteca.model.UserAccount;
import com.twu.biblioteca.service.LibraryServiceManager;
import com.twu.biblioteca.model.ServiceResult;
import com.twu.biblioteca.utils.AssetGenerator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class BibliotecaApp {

    private static UserAccount defaultAccount = AssetGenerator.createDefaultUser();

    public static void main(String[] args) {

        Library library = AssetGenerator.generateStockedLibrary();

        LibraryServiceManager serviceManager = new LibraryServiceManager(library);
        serviceManager.setUser(defaultAccount);
        List<LibraryService> libraryServices = serviceManager.getLibraryServices();

        System.out.println("Welcome to the library, " + defaultAccount.getName() + "!");

        while( true ){
            System.out.println("Select from the following services:");
            displayLibraryServices(libraryServices);

            System.out.print("Enter your option: ");
            Integer input = getIntegerFromConsole(0,libraryServices.size());
            if( input < libraryServices.size() ) {
                ServiceResult serviceResult = libraryServices.get(input).performService();
                System.out.println(serviceResult.getResult());
            }else{
                System.out.println("Thank you! We hope to see you again!");
                break;
            }

            askUserToPressEnter();
        }
    }

    /**
     * Displayes Main Menu
     * @param libraryServices lists all services in the library
     */
    private static void displayLibraryServices(List<LibraryService> libraryServices) {
        int index;
        for( index=0; index<libraryServices.size(); index++){
            System.out.println("[" + index + "] " + libraryServices.get(index).getName() );
        }

        System.out.println("[" + index + "] Quit");
    }

    /**
     * Gets integer value from console.
     *
     * @param maxInt maximum allowable Integer value
     * @return the Integer value
     */
    private static Integer getIntegerFromConsole(int minInt, int maxInt) {
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));

        while( true ) {
            try {
                Integer integer = Integer.parseInt(bufferRead.readLine());
                if (integer >= minInt && integer <= maxInt) {
                    return integer;
                } else {
                    showInvalidOptionMessage();
                }
            } catch (Exception e) {
                showInvalidOptionMessage();
            }
        }
    }

    private static void showInvalidOptionMessage(){
        System.out.print("Select a valid option!\n");
    }

    private static void askUserToPressEnter() {
        System.out.print("Press <ENTER> to continue...");
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
